package softtech.apps.callblocker.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import softtech.apps.callblocker.R;
import softtech.apps.callblocker.model.HeaderSMSBlockedItem;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AdapterMessengerHistory extends BaseAdapter{

	ArrayList<HeaderSMSBlockedItem> MessengerListHeader = new ArrayList<HeaderSMSBlockedItem>();
	LayoutInflater inf;
	Context context;
	
	//display date
    LinearLayout.LayoutParams lparamDate;
    //display date and number
    LinearLayout.LayoutParams lparamMessenger ;//= new  LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
    
	public AdapterMessengerHistory(ArrayList<HeaderSMSBlockedItem> MessengerListHeader,
			LayoutInflater inf
			,Context context){
		this.MessengerListHeader = MessengerListHeader;
		this.inf = inf;
		this.context = context;
	}
	/**
	* display 30 item
	*/
	public int count =30;
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(MessengerListHeader.size()>=count)
		{
			return count;
		}
		if(MessengerListHeader.size()<=0){
			return 1;
		}
		return MessengerListHeader.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * update list sms block
	 * @param item
	 */
	public void update(ArrayList<HeaderSMSBlockedItem> item){
		MessengerListHeader = item;
		notifyDataSetChanged();
	}
	@Override
	public View getView(int position, View vi, ViewGroup parent) {

		if (MessengerListHeader == null || MessengerListHeader.equals(null)
				|| MessengerListHeader.size() == 0) {
			vi = inf.inflate(R.layout.item_search_notfound,
					null);
			return vi;
		}
		
		ViewHolder viewholder = new ViewHolder();
		if(vi==null){
        vi = inf.inflate(R.layout.item_call_history_messenger_item, null);
        //create component
        fCreateComponent(vi,viewholder);
        vi.setTag(viewholder);
		}
		else{
			viewholder = (ViewHolder) vi.getTag();
		}
        if(MessengerListHeader.get(position).getPointSection()==1){
        viewholder.tvContent.setText(""+fDisplayDate(position,viewholder));
        }
        else{
       	//Display Number
            fDisplayNumberAndDate(position,viewholder);
        }
        return vi;
	}
	/**
	 * View Holder
	 */
	class ViewHolder{
		TextView tvContent ;
	    TextView tvNumber ;
	    ImageView ivSMS ;
	    TextView tvTime ;
	    RelativeLayout relativeLayout ;
	}
	/**
	 * Section date: ToDay
	 * 
	 * @param date
	 * @param formattedDate
	 */
	public int setDateToday(String date){
		Calendar c = Calendar.getInstance();
 		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 		String formattedDate = df.format(c.getTime());
 		 if(date.equals(formattedDate)){
 			 return 1;
 		 }
 		 return 0;
	}
	/**
	 * Section date: yesterday
	 * 
	 * @param date
	 * @param formattedDate
	 * @return
	 */
	public int setDateYesterday(String date){
		Calendar c = Calendar.getInstance();
 		c.add(Calendar.DATE, -1);
 		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 		String formattedDate = df.format(c.getTime());
 		if(date.equals(formattedDate)){
			 return 1;
		 }
// 		hoai
		 return 0;
	}
	/**
	 * recall phone number
	 * @param id
	 */
	private void fRecall(int id){
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.setData(Uri.parse("tel:"+MessengerListHeader.get(id).getPhoneNumber()));
		context.startActivity(callIntent);
	}
	/**
	 * Create component
	 */
	private void fCreateComponent(View vi,ViewHolder view){
		// display date
		lparamDate = new LinearLayout.LayoutParams(0, 0);
		// display date and number
		lparamMessenger = new LinearLayout.LayoutParams(
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT);
		view.tvContent = (TextView) vi.findViewById(R.id.tvMessengerContentHistory);
		view.tvNumber = (TextView) vi.findViewById(R.id.tvMessengerNumberHistory);
		view.ivSMS = (ImageView) vi.findViewById(R.id.ivMessengerHistory);
		view.tvTime = (TextView) vi.findViewById(R.id.tvMessengerTime);
		view.relativeLayout = (RelativeLayout) vi
				.findViewById(R.id.layoutMessengerhistory);
	}
	/**
	 * convert datetime to date
	 * @param date
	 */
	private String stringToDate(String day){
		Date date = null;
		SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
		try {  
		     date = format.parse(day);  
		    System.out.println(date);  
		} catch (ParseException e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		SimpleDateFormat  dateformat = new SimpleDateFormat("yyyy-MM-dd");  
		String day1=null;
		try {  
		  
		 day1= dateformat.format(date);
		} catch (Exception e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		return day1;
	}
	/**
	 * convert datetime to time
	 * @param time
	 * @return
	 */
	private String stringToTime(String time){
		Date date = null;
		SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
		try {  
		     date = format.parse(time);  
		    System.out.println(date);  
		} catch (ParseException e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		SimpleDateFormat  dateformat = new SimpleDateFormat("HH:mm");  
		String time1 =null;
		try {  
		  
		 time1= dateformat.format(date);
		} catch (Exception e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		return time1;
	}
	/**
	 * Display date
	 */
	private String fDisplayDate(int id, ViewHolder view){
		view.tvNumber.setLayoutParams(lparamDate);
		view.tvContent.setLayoutParams(lparamMessenger);
		view.tvContent.setTypeface(Typeface.DEFAULT_BOLD);
		view.tvContent.setTextSize(15);
		view.ivSMS.setLayoutParams(lparamDate);
		view.tvTime.setLayoutParams(lparamDate);
		view.relativeLayout.setBackgroundColor(Color.parseColor("#1d1e23"));
    	 String getdatetime = MessengerListHeader.get(id).getDateTime();
    	 if(setDateToday(stringToDate(getdatetime))==1){
    		 return "Today";
    	 }
    	 else if(setDateYesterday(stringToDate(getdatetime))==1){
    		 return "Yesterday";
    	 }
    	 else 
    	 return stringToDate(getdatetime);
	}
	/**
	 * Display phone number
	 * @param position
	 */
	private void fDisplayNumberAndDate(int position,ViewHolder view){
		view.tvTime.setLayoutParams(lparamMessenger);
		view.ivSMS.setLayoutParams(lparamMessenger);
		view.tvNumber.setLayoutParams(lparamMessenger);
		view.tvNumber.setText(MessengerListHeader.get(position).getPhoneNumber());
		view.tvContent.setTextSize(10);
		view.tvContent.setFocusable(false);
		view.tvContent.setText("content: "+MessengerListHeader.get(position).getContent().toString());
		view.relativeLayout.setBackgroundColor(Color.parseColor("#3d3f4b"));
       	String getdatetime = MessengerListHeader.get(position).getDateTime();
      	
        
       	view.tvTime.setText(""+stringToTime(getdatetime));
		//
		// set model
		//
       	int model = MessengerListHeader.get(position).getStatus();
       	switch (model) {
		case 1:
			view.ivSMS.setBackgroundResource(R.drawable.modesms1);
			break;
		case 2:
			view.ivSMS.setBackgroundResource(R.drawable.modesms2);
			break;
		case 3:
			view.ivSMS.setBackgroundResource(R.drawable.modesms3);
			break;

		default:
			break;
		}
       	
	}

}
